package com.yonyou.pmclouds.organization.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.organization.entity.OrganizationVO;
import com.yonyou.pmclouds.organization.rmiitf.OrgOutterMaintain;
import com.yonyou.pmclouds.organization.rmiitf.OrganizationMaintain;
import com.yonyou.pmclouds.outter.role.RoleInfoQuery;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping(value = "/outter/organization")
public class OrgOutterController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private OrganizationMaintain organizationMaintain;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private OrgOutterMaintain orgOutterMaintain;

    @Autowired
    private RoleInfoQuery roleInfoQuery;


    @ResponseBody
    @RequestMapping(value = "/insertFromOutter")
    public OrganizationVO insertFromOutter(@RequestBody OrganizationVO orgVO,
                                  HttpServletRequest request) throws BusinessException{
        String pkTenant = request.getHeader("accountId");

        if(StringUtils.isEmpty(pkTenant))
            throw new BusinessException("未找到对应租户信息");
        RuntimeEnvironment.setTenantId(pkTenant);
        orgVO.setPkTenant(pkTenant);
        orgVO.setEnableState(2);

        OrganizationVO[] orgs = organizationMaintain.insertOrgs(new OrganizationVO[]{orgVO});

        return orgs[0];
    }

    @ResponseBody
    @RequestMapping(value = "/updateFromOutter")
    public OrganizationVO updateFromOutter(@RequestBody OrganizationVO orgVO,
                                           HttpServletRequest request) throws BusinessException{
        String pkTenant = request.getHeader("accountId");

        if(StringUtils.isEmpty(pkTenant))
            throw new BusinessException("未找到对应租户信息");

        RuntimeEnvironment.setTenantId(pkTenant);
        orgVO.setPkTenant(pkTenant);

        return orgOutterMaintain.updateOrgFromOutter(orgVO);
    }

    @ResponseBody
    @RequestMapping(value = "/deleteFromOutter")
    public OrganizationVO deleteFromOutter(@RequestBody OrganizationVO orgVO,
                                           HttpServletRequest request) throws BusinessException{
        String pkTenant = request.getHeader("accountId");

        if(StringUtils.isEmpty(pkTenant))
            throw new BusinessException("未找到对应租户信息");

        RuntimeEnvironment.setTenantId(pkTenant);
        orgVO.setPkTenant(pkTenant);

        return orgOutterMaintain.deleteOrgFromOutter(orgVO);
    }

    @ResponseBody
    @RequestMapping(value = "/enableFromOutter")
    public OrganizationVO enableFromOutter(@RequestBody OrganizationVO orgVO,
                                           HttpServletRequest request) throws BusinessException{
        String pkTenant = request.getHeader("accountId");

        if(StringUtils.isEmpty(pkTenant))
            throw new BusinessException("未找到对应租户信息");

        RuntimeEnvironment.setTenantId(pkTenant);
        orgVO.setPkTenant(pkTenant);

        return orgOutterMaintain.update4Enable(orgVO);
    }

    @ResponseBody
    @RequestMapping(value = "/disableFromOutter")
    public OrganizationVO disableFromOutter(@RequestBody OrganizationVO orgVO,
                                           HttpServletRequest request) throws BusinessException{
        String pkTenant = request.getHeader("accountId");

        if(StringUtils.isEmpty(pkTenant))
            throw new BusinessException("未找到对应租户信息");

        RuntimeEnvironment.setTenantId(pkTenant);
        orgVO.setPkTenant(pkTenant);

        if(orgVO == null)
            throw new BusinessException("传入组织为空");

        List<Map> roleList = roleInfoQuery.queryRolesByTenant(RuntimeEnvironment.getTenantId());
        Set<String> recordRole = new HashSet<>();
        if(roleList != null){
            for(Map map : roleList){
                if(map.get("roleId") != null) recordRole.add((String)map.get("roleId"));
            }
        }
        return orgOutterMaintain.update4Disable(orgVO, recordRole);
    }

}
